package midder

import (
	"api-gateway/basic/config"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Auth() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.Request.Header.Get("token")
		if token == "" {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  "未登录",
				"data": false,
			})
			return
		}
		// 设置 example 变量
		key := fmt.Sprintf("token")
		result, _ := config.Rdb.Get(config.Ctx, key).Result()
		if result == "" {
			c.JSON(http.StatusBadRequest, gin.H{
				"code": 400,
				"msg":  "未登录2",
				"data": false,
			})
			return
		}
		c.Set("userId", result)
		c.Next()
	}
}
